package br.com.onlinebidding.mb;

import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import br.com.onlinebidding.facade.ProductFacade;
import br.com.onlinebidding.model.Product;

@SessionScoped
@ManagedBean(name = "productMB")
public class ProductMB extends AbstractMB implements Serializable {

	private static final long serialVersionUID = 1L;
	public static final String INJECTION_NAME = "#{productMB}";

	private Product product;
	private List<Product> products;
	private ProductFacade productFacade;
	public List<Product> notUse = null;
	public List<Product> result = null;
	
	public ProductMB(){
		
	}

	public ProductFacade getProductTFacade() {
		if (this.productFacade == null) {
			this.productFacade = new ProductFacade();
		}
		return this.productFacade;
	}

	public void insertProduct() {
		try {
			getProductTFacade().insertProduct(this.product);
			closeDialog();
			displayInfoMessageTo("Inserido Com Sucesso!");
			loadProducts();
			resetProduct();
		} catch (Exception e) {
			keepDialogOpen();
			displayErrorMessageTo("Ops, erro ao inserir . Tente novamente mais tarde!");
			e.printStackTrace();
		}
	}

	public void updateProduct() {
		try {
			getProductTFacade().updateProduct(this.product);
			closeDialog();
			displayInfoMessageTo("Alterado Com Sucesso!");
			loadProducts();
			resetProduct();
		} catch (Exception e) {
			keepDialogOpen();
			displayErrorMessageTo("Ops, erro ao alterar . Tente novamente mais tarde!");
			e.printStackTrace();
		}
	}

	public void deleteProduct() {
		try {
			getProductTFacade().deleteProduct(this.product);
			closeDialog();
			displayInfoMessageTo("Excluido Com Sucesso!");
			loadProducts();
			resetProduct();
		} catch (Exception e) {
			keepDialogOpen();
			displayErrorMessageTo("Ops, erro ao excluir  . Tente novamente mais tarde!");
			e.printStackTrace();
		}
	}
	
	public List<Product> getlistAllNotUse() {
		
			this.notUse = getProductTFacade().listAllProductsNotUse();			
		
			return this.notUse;
	}

	public List<Product> getAllProducts() {
		if (this.products == null) {
			loadProducts();
		}
		return this.products;
	}

	

	private void loadProducts() {
		this.products = getProductTFacade().listAll();
	}

	
	public List<Product> getNotUse() {
		return notUse;
	}

	public void setNotUse(List<Product> notUse) {
		this.notUse = notUse;
	}

	public void resetProduct() {
		this.product = new Product();
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

	public ProductFacade getProductFacade() {
		return productFacade;
	}

	public void setProductFacade(ProductFacade productFacade) {
		this.productFacade = productFacade;
	}
}
